/*
 * Task.cpp
 *
 *  Created on: 19.11.2012
 *      Author: Lapchinskiy
 */
#include "BuildConfig.h"
#include "Global_types.h"
#include "Logging.h"
#include "ArrayList.h"
#include "ConditionVariable.h"
#include "Condition.h"
#include "Task.h"

using namespace std;
//Task::Task(char* name, ErrorCode (*callBack)(void *args),
//		Condition<int> *conditonToRun) {
//	Task(name, callBack);
//	this->conditionToRun = conditonToRun;
//}
#ifdef GLOBAL_LOGGING_ENABLE
ModuleType Task::moduleType = TASK;
#endif //GLOBAL_LOGGING_ENABL
uint8_t Task::taskCounter = 0;

Task* Task::run() {
	if ((status == ENABLED)
			&& ((conditionToRun == 0) || (conditionToRun->check() == true))) {
#ifdef GLOBAL_LOGGING_ENABLE
#ifdef TASK_LOGGING_ENABLE
		Log(&logrec, errorCode, (char*) "started!");
#endif //TASK_LOGGING_ENABLE
#endif //GLOBAL_LOGGING_ENABLE
		errorCode = callbackFunc(callbackArgs);
#ifdef GLOBAL_LOGGING_ENABLE
#ifdef TASK_LOGGING_ENABLE
		Log(&logrec, errorCode, (char*) "executed!");
#endif //GLOBAL_LOGGING_ENABLE
#endif //GLOBAL_LOGGING_ENABLE
		return nextTask;
	}
	return this;
}

Task::Task(char* name, ErrorCode (*callBack)(void *args), void *args,
		TaskStatus enabled) {
	this->status = enabled;
	this->id = Task::taskCounter++;
	this->callbackFunc = callBack;
	this->conditionToRun = NULL;
	this->callbackArgs = args;
	this->nextTask = this;
	errorCode = NO_ERR;
#ifdef GLOBAL_LOGGING_ENABLE
#ifdef GLOBAL_LOGGING_ENABLE
	logrec.module = TASK;
	logrec.objectName = name;
#endif //GLOBAL_LOGGING_ENABLE
#endif //GLOBAL_LOGGING_ENABLE
#ifdef GLOBAL_LOGGING_ENABLE
	Log(&logrec, errorCode, (char*) "created!");
#endif //GLOBAL_LOGGING_ENABLE
}

Task::Task(char* name, ErrorCode (*callBack)(void *args), void *args,
		Condition<uint32_t> *cond, TaskStatus enabled) {
	this->status = enabled;
	this->id = Task::taskCounter++;
	this->callbackFunc = callBack;
	this->conditionToRun = cond;
	this->callbackArgs = args;
	this->nextTask = this;
	errorCode = NO_ERR;
#ifdef GLOBAL_LOGGING_ENABLE
#ifdef GLOBAL_LOGGING_ENABLE
	logrec.module = TASK;
	logrec.objectName = name;
#endif //GLOBAL_LOGGING_ENABLE
#endif //GLOBAL_LOGGING_ENABLE
#ifdef GLOBAL_LOGGING_ENABLE
	Log(&logrec, errorCode, (char*) "created!");
#endif //GLOBAL_LOGGING_ENABLE
}
Task::Task() {

}

ErrorCode Task::addNextTask(Task* nextTask) {
	this->nextTask = nextTask;
	return errorCode;
}

ErrorCode Task::Enable(void) {
	status =ENABLED;
	return errorCode;
}

ErrorCode Task::Disable(void) {
	status =DISABLED;
	return errorCode;
}

TaskStatus Task::GetStatus(void) {
	return status;
}

Task::~Task() {

}
